using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;

namespace SsisUtilities.PacMan.Components
{    
    public class ValidationHelper : IDTSEvents
    {
        private Package ssisPackage;
        
        private List<string> infoMessages = new List<string>();
        private List<string> warningMessages = new List<string>();
        private List<string> errorMessages = new List<string>();

        #region Public Properties

        public List<string> InfoMessages
        {
            get
            {
                return infoMessages;
            }
        }

        public List<string> WarningMessages
        {
            get
            {
                return warningMessages;
            }
        }

        public List<string> ErrorMessages
        {
            get
            {
                return errorMessages;
            }
        }

        #endregion

        public ValidationHelper(Package ssisPackage)
        {
            this.ssisPackage = ssisPackage;
        }

        public DTSExecResult Validate()
        {
            return ssisPackage.Validate(ssisPackage.Connections, 
                ssisPackage.Variables, this, null);
        }

        #region Implemented IDTSEvents Members

        void IDTSEvents.OnWarning(DtsObject source, int warningCode, string subComponent, string description, string helpFile, int helpContext, string idofInterfaceWithError)
        {
            warningMessages.Add(description);
        }

        bool IDTSEvents.OnError(DtsObject source, int errorCode, string subComponent, string description, string helpFile, int helpContext, string idofInterfaceWithError)
        {
            errorMessages.Add(description);
            return false;
        }

        void IDTSEvents.OnInformation(DtsObject source, int informationCode, string subComponent, string description, string helpFile, int helpContext, string idofInterfaceWithError, ref bool fireAgain)
        {
            infoMessages.Add(description);
        }

        #endregion

        #region Empty Shell (unimplemented) IDTSEvents Members

        void IDTSEvents.OnBreakpointHit(IDTSBreakpointSite breakpointSite, BreakpointTarget breakpointTarget)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void IDTSEvents.OnCustomEvent(TaskHost taskHost, string eventName, string eventText, ref object[] arguments, string subComponent, ref bool fireAgain)
        {
            //throw new Exception("The method or operation is not implemented.");
        }



        void IDTSEvents.OnExecutionStatusChanged(Executable exec, DTSExecStatus newStatus, ref bool fireAgain)
        {
            //throw new Exception("The method or operation is not implemented.");
        }



        void IDTSEvents.OnPostExecute(Executable exec, ref bool fireAgain)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void IDTSEvents.OnPostValidate(Executable exec, ref bool fireAgain)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void IDTSEvents.OnPreExecute(Executable exec, ref bool fireAgain)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void IDTSEvents.OnPreValidate(Executable exec, ref bool fireAgain)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void IDTSEvents.OnProgress(TaskHost taskHost, string progressDescription, int percentComplete, int progressCountLow, int progressCountHigh, string subComponent, ref bool fireAgain)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        bool IDTSEvents.OnQueryCancel()
        {
            //throw new Exception("The method or operation is not implemented.");
            return false;
        }

        void IDTSEvents.OnTaskFailed(TaskHost taskHost)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        void IDTSEvents.OnVariableValueChanged(DtsContainer DtsContainer, Variable variable, ref bool fireAgain)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }

    /// <summary>
    /// Simple helper class to encapsulate the validation 
    /// results for easier consumption in the PacMan UI.
    /// </summary>
    public class PackageValidationResults
    {
        private string packageName;
        private DTSExecResult results;
        private string message;
        
        public PackageValidationResults(string packageName,
            DTSExecResult results, string message)
        {
            this.packageName = packageName;
            this.results = results;
            this.message = message;
        }

        public string PackageName
        {
            get
            {
                return this.packageName;
            }
        }

        public DTSExecResult Results
        {
            get
            {
                return this.results;
            }
        }

        public string Message
        {
            get
            {
                return this.message;
            }
        }

        public string ResultDescription
        {
            get
            {
                return this.results.ToString();
            }
        }
    }

}
